
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.bean.ReceiptBean;
import com.ukao.steward.bean.StorageListBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.L;
import com.ukao.steward.view.valetRunners.StorageListView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class StorageListPesenter extends BasePresenter<StorageListView> {


    public StorageListPesenter(StorageListView view) {
        attachView(view);
    }






    /**
     绑卡
     */
    public void queryOrder( String  keywords,String  storeId) {

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("keywords",keywords);
        if (!CheckUtils.isEmpty(storeId))
            mHashMap.put("storeId",storeId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.queryOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StorageListBean>() {
                    @Override
                    public void onSuccess(StorageListBean model) {

                        if (model.getHttpCode() == 200) {
                             mvpView.queryOrderSuccceed(model.getData());
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.queryOrderFinish();

                    }
                });

    }

    /**
     * 完成入库
     * @param orderId
     */
    public void stockInfinish( String  orderId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.stockInfinish(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.stockInfinishSucceed();
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                    mvpView.hideLoading();

                    }
                });

    }

    /**
     *  修改订单金额
     * @param orderId
     * @param modifyPrice 修改金额
     */
    public void storageModifyPrice( String  orderId,String  modifyPrice) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",orderId);
        mHashMap.put("modifyPrice", DecimalUtil.multiply(modifyPrice));
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.storageModifyPrice(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.storageModifyPrice(DecimalUtil.multiply(modifyPrice,"1"));
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }

    /**
     *打印订单
     */
    public void printReceipt( String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.showError(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }

    /**
     *复制衣物
     */
    public void copy( String  proId ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("proId",proId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.copy(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.codeSucceed();
                        }else{
                            mvpView.showError(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }
    /**
     *删除衣物
     */
    public void delete( StorageListBean.ProListBean  mItem ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("id",mItem.getId());
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.delete(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.deleteSucceed(mItem);
                        }else{
                            mvpView.showError(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }




    /**
     * 支付解锁
     * @param orderId
     */
    public void unlockOrder(String orderId      ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                            payInfo(orderId);

                        } else {

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                    }

                });
    }

    /**
     * 支付订单信息
     *
     * @param id
     */
    public void payInfo(String  id ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap.toString());
        mvpView.showLoading();
        addSubscription( apiStores.payInfo(Constant.createParameter(mHashMap)),
                new ApiCallback<OrderInfoBean>() {
                    @Override
                    public void onSuccess(OrderInfoBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.payOrderInfoData(model.getData());


                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }




}
